package com.hhsjtest.composiable.logtest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

import java.lang.reflect.Field;

public class LogScrollView extends NestedScrollView {
    public LogScrollView(@NonNull Context context) {
        super(context);
    }

    public LogScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LogScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.e("HHSJ", "----------------------------LinearLayout " + getChildAt(0).hashCode());
        Log.e("HHSJ", "Horizontal " + ((ViewGroup) getChildAt(0)).getChildAt(0).hashCode());
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            Class<ViewGroup> viewGroupClass = ViewGroup.class;
            try {
                Field field = viewGroupClass.getDeclaredField("mFirstTouchTarget");
                field.setAccessible(true);
                Object o = field.get(this);
                if (o == null) {
                    Log.e("HHSJ", " mFirstTouchTarget = null");
                } else {
                    Log.e("HHSJ", " mFirstTouchTarget = " + o.hashCode());

                    Field field1 = o.getClass().getField("child");
                    field1.setAccessible(true);
                    Object child = field1.get(o);
                    if (child != null) {
                        Log.e("HHSJ", "TouchTarget child " + child.hashCode());
                    } else {
                        Log.e("HHSJ", "TouchTarget child = null");
                    }
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            boolean intercept = super.onInterceptTouchEvent(ev);
            Log.e("HHSJ", "down intercept " + intercept);
            return intercept;
        }
        if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            boolean intercept = super.onInterceptTouchEvent(ev);
            Log.e("HHSJ", "move intercept " + intercept);
            return intercept;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            boolean intercept = super.onTouchEvent(ev);
            Log.e("HHSJ", "move touch " + intercept);
            return intercept;
        }
        return super.onTouchEvent(ev);
    }
}
